/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.common.util;

import com.koron.common.bean.RoleBean;
import com.koron.common.web.Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class OperateTest {
	
	@Value("${adminRoles}")
    private String adminRoles;
	
    @Autowired
    private Util util;
    
	@SuppressWarnings("unchecked")
	public boolean isAdmin(String userCode){
		if(StringUtils.isBlank(userCode)){
			return false;
		}
		if(StringUtils.isBlank(adminRoles)){
			return false;
		}
		String[] roleArray = adminRoles.split(",");
		if(roleArray.length<=0){
			return false;
		}
		Map<String,Object> map= (Map<String, Object>) util.getRolesAndGroupsByStaff(userCode,1024).getData();
        if (null!=map&&!map.isEmpty()){
            List<RoleBean> roleBeanList= (List<RoleBean>) map.get("role");
            List<String> roleCodeList=new ArrayList<>();
            if (null!=roleBeanList&&roleBeanList.size()>0){
                roleCodeList=roleBeanList.stream().map(RoleBean::getCode).collect(Collectors.toList());
                for (String string : roleCodeList) {
					if(Arrays.asList(roleArray).contains(string)){
						return true;
					}
				}
            }
        }
		return false;
	}
}
